package com.mytest.leetcode;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

@Slf4j
public class Solution557_3 {

    public static void main(String[] args) {
        Solution557_3 solution344 = new Solution557_3();
        String s = "I love u";
        solution344.reverseWords(s);
        log.info("{}", solution344.reverseWords1(s));
    }
    public String reverseWords(String s) {

        String[] ss = s.split(" ");
        int left = 0;
        int right = ss.length -1;
        StringBuffer ret = new StringBuffer();
        while(left < right +1){
            String leftS = changeChar(ss[left]);
            ss[left] = leftS;

            if (left == right) {
                ret.append(leftS);
            } else {
                ret.append(leftS+" ");
            }
            left++;
//            String rightS = changeChar(ss[right]);
//            ss[right] = rightS;
//            right--;

        }
        return ret.toString();
    }


    public String reverseWords1(String s) {

        String[] ss = s.split(" ");
        int left = 0;
        int right = ss.length -1;
        StringBuffer ret = new StringBuffer();
        while(left < right+1){
            String leftS = changeChar(ss[left]);
            ss[left] = leftS;
            left++;
            if(left < right){
                String rightS = changeChar(ss[right]);
                ss[right] = rightS;
                right--;
            }
        }
        for(int i = 0; i< ss.length;i++){
            if(i== ss.length -1){
                ret.append(ss[i]);
            } else {
                ret.append(ss[i]+" ");
            }

        }
        return ret.toString();
    }




    public String changeChar(String s){
        char [] charS = s.toCharArray();
        int left = 0;
        int right = charS.length - 1;
        while(left < right){
            char temp = charS[left];
            charS[left] = charS[right];
            charS[right] = temp;
            left++;
            right--;
        }
        return new String(charS);
    }
}
